package org.glut.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.glut.backend.domain.Homepage;
import org.glut.backend.service.IHomepageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 主页表 前端控制器
 *
 * @author fengyue
 * @since 2025-08-04 16:20
 */
@RestController
@RequestMapping("/public/homepage")
public class HomepageController {

    @Autowired
    private IHomepageService homepageService;

    @GetMapping("/query/{id}")
    public ResponseEntity<Homepage> getById(@PathVariable Integer id) {
        Homepage homepage = this.homepageService.getById(id);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token", "kun");
        return new ResponseEntity<Homepage>(homepage, httpHeaders, HttpStatus.OK);
    }


    @GetMapping("/queryall")
    public ResponseEntity<List<Homepage>> getAll() {
    List<Homepage> homepages = homepageService.list();
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("token", "kun");
        return new ResponseEntity<>(homepages, httpHeaders, HttpStatus.OK);
    }

    @GetMapping("/query/search")
    public List<Homepage> querysearch(String input) {
        // Lambda 方式构建查询条件（更安全，避免字段名拼写错误）
        LambdaQueryWrapper<Homepage> lambdaWrapper = new LambdaQueryWrapper<>();

        // 姓名包含 keyword（使用 User::getName 引用字段）
        lambdaWrapper.like(Homepage::getName, input)  //名字模糊查询
                .or()  //或者

                .like(Homepage::getDateLaunch,input);  //时间模糊查询
        return homepageService.list(lambdaWrapper);
    }
}
